var webpack = require('webpack')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var HtmlWebpackPlugin = require('html-webpack-plugin')
const path = require('path')
const ROOT_PATH = path.resolve(__dirname)
var config = {
  devtool: false,
  entry: {
    'app': ['./app/index.jsx']
  },
  output: {
    path: __dirname + '/dist/',
    publicPath: '/dist/',
    filename: 'js/[name].[chunkhash:8].js',
    chunkFilename: 'js/[name].[chunkhash:5].chunk.js'
  },
  resolve: {
    extensions: ['.js', '.jsx']
  },
  module: {
    loaders: [
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract({
          use: 'css-loader',
          fallback: 'style-loader'
        })
      },
      {test: /\.(gif|jpg|png|woff|svg|eot|ttf)\??.*$/, loader: 'url-loader?limit=20&name=resource/[name].[ext]'},
      {test: /\.scss$/,
        loader: ExtractTextPlugin.extract({
          use: 'css-loader!sass-loader',
          fallback: 'style-loader'
        })
      },
      {
        test: /\.js?$/,
        exclude: /(node_modules)/,
        loader: 'babel-loader'
      },
      {
        test: /\.jsx?$/,
        exclude: '/(node_modules)/',
        loader: 'babel-loader'
      }
    ]

  },
  plugins: [
    new webpack.optimize.UglifyJsPlugin({
      output: {
        comments: false // remove all comments
      },
      compress: {
        warnings: false
      }
    }),
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify('production')
      }
    }),
    // new webpack.optimize.AggressiveMergingPlugin(),//合并块
    new webpack.DllReferencePlugin({
      manifest: require(path.resolve(ROOT_PATH, 'dist/lib', 'manifest.json')),
      context: ROOT_PATH
    }),
    // 单独处理css
    new ExtractTextPlugin('css/[name].[contenthash:8].css'),
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: './app/index.html',
      inject: true,
      hash: true,
      chunks: ['vendor', 'app'],
      chunksSortMode: 'dependency',
      minify: {
        removeComments: true,
        collapseWhitespace: false
      }
    }),
    new HtmlWebpackPlugin({
      filename: 'server_template.ejs',
      template: '!!ejs-compiled-loader!./app/server_template.ejs',
      inject: true,
      hash: true,
      chunks: ['vendor', 'app'],
      chunksSortMode: 'dependency',
      minify: {
        removeComments: true,
        collapseWhitespace: false
      }
    })
  ]
}

module.exports = config
